<?php
/**
 * @Author: Awe
 * @Date:   2018-09-30 13:01:43
 * @Last Modified by:   Awe
 * @Last Modified time: 2018-10-10 12:13:52
 */
class RechargeApi{
    public static $username = '98137775';
    public static $apikey = '75RL8r0T2ss2e1FgtyAb';
    /** 
    * 话费充值
    * @access public 
    * @param mobile 需要充值的手机号码
    * @param package 充值金额 支持1元、2元、3元、4元、5元、6元、7元、8元、9元、10元、20元、30元、50元、100元、200元、300元、500元。
    * @return boolean
    */
    public static function recharge($mobile , $package){
        $log = Log_file::getInstance(array('filename' => "recharge.log" ));
        $basicUrl = "http://api.ihuyi.com/f/phone?action=recharge&%s";
        $username = self::$username;
        $apikey = self::$apikey;
        $orderid = 'bird_'.date("YmdHis").mt_rand(100, 1000);
        $dataGet = array();
        $dataGet['package'] = $package;
        $dataGet['username'] = $username;
        $dataGet['timestamp'] = date("YmdHis");
        $dataGet['mobile'] = $mobile;
        $dataGet['orderid'] = $orderid;
        $dataGet['sign'] =
        md5(sprintf("apikey=%s&mobile=%s&orderid=%s&package=%s&timestamp=%s&username=%s",
         $apikey,
         $mobile,
        $orderid,
         $package,
         date("YmdHis"),
         $username));
        $dataReturn = array();
        foreach ($dataGet as $key => $row) {
            $dataReturn[] = sprintf("%s=%s", $key, $row);
        }
        $urlGet = sprintf($basicUrl, implode("&", $dataReturn));
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $urlGet); //定义表单提交地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //60 秒
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_REFERER, 'http://' . $_SERVER['HTTP_HOST']);
        curl_setopt($ch, CURLOPT_POST, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        $dataRet = json_decode($data, 1);
        if( $dataRet['code'] == 1 ){
            return array('code' => 1 ,'msg' => "ok") ;
        }

        $log->Write("error" , "充值失败：" .$data);
        return array('code' => 0 ,'msg' => $data) ;
    }
}